FUNCTION ZFIFM_VIM_DETERM_PROC_155 .
*"--------------------------------------------------------------------
*"*"局部接口：
*"  CHANGING
*"     REFERENCE(INDEX_DATA) TYPE  /OPT/VIM_1HEAD OPTIONAL
*"  EXCEPTIONS
*"      CHECK_FAILED
*"--------------------------------------------------------------------
  DATA : L_WAERS LIKE INDEX_DATA-WAERS.
  CLEAR L_WAERS.
**>>>Begin of Changes 560/2
***Types
  TYPES  : BEGIN OF TY_ITEM,
             DOCID     TYPE /OPT/VIM_1ITEM-DOCID,
             ITEMID    TYPE /OPT/VIM_1ITEM-ITEMID,
             EBELN     TYPE /OPT/VIM_1ITEM-EBELN,
             EBELP     TYPE EBELP,                             "VIMI-19726
             COND_TYPE TYPE KSCHL,                          "VIMI-19726
           END OF TY_ITEM.
  TYPES : BEGIN OF TY_EKKO,
            EBELN TYPE EKKO-EBELN,
            WAERS TYPE EKKO-WAERS,
            KNUMV TYPE EKKO-KNUMV,                         "VIMI-13235
          END OF TY_EKKO.
***DATA delcariton for item validation
  DATA : LT_ITEM TYPE STANDARD TABLE OF TY_ITEM,
         LT_EKKO TYPE STANDARD TABLE OF TY_EKKO.
***Worke area for item Validation
  DATA : WA_ITEM TYPE TY_ITEM,
         WA_EKKO TYPE TY_EKKO.

*Begin of VIMI-8960 - cnicklas
  TYPES : BEGIN OF TY_PO,
            EBELN TYPE EKKO-EBELN,
          END OF TY_PO.
  DATA : LT_PO TYPE STANDARD TABLE OF TY_PO.
  DATA : WA_PO TYPE TY_PO.
  DATA: LV_VALUE TYPE /PTGWFI/VALUE.
  DATA: LT_LIST TYPE TABLE OF /OPT/VIM_1PO_DN,
        LS_LIST TYPE /OPT/VIM_1PO_DN.
*End of VIMI-8960 - cnicklas

  DATA: LV_KNUMV   TYPE KNUMV,                            "VIMI-13235
        LV_CNDTYPE TYPE KSCHL,                            "VIMI-13235
        LV_RAISE   TYPE FLAG.                             "VIMI-13235
** Begin of Insertion: VIMI-28119
  DATA: LV_EBELP    TYPE EBELP,
        LV_FR_RAISE TYPE FLAG.
** End   of Insertion: VIMI-28119
  DATA: LS_SW_VERS  TYPE CVERS.                             "VIMI-16564
  DATA: LT_KONV     TYPE TABLE OF KONV.                     "VIMI-16564
  DATA: LS_KONV     TYPE KONV.                              "VIMI-16564
  DATA: LS_FM_NAME  TYPE TFDIR-FUNCNAME.                    "VIMI-16564
  DATA: LV_COND_FLG TYPE CHAR1.                             "VIMI-31377
*Begin S4Hana Change - VIMI-16564
* Start of INS VIMI-19726
  DATA: LT_EK08BN    TYPE TABLE OF EK08BN,
        WA_EK08BN    TYPE EK08BN,
        LV_GSTFLG    TYPE CHAR1,
        LV_SUBRC     TYPE SY-SUBRC,
        LV_RAISEFLAG TYPE CHAR1.

  " Add by roc.wang 海关发票不做该检查
  SELECT COUNT(*)
    FROM ZFIT078_HEAD
   WHERE ZFPHM = INDEX_DATA-LXBLNR
     AND INVOICE_LINE LIKE '%17%'.
  IF SY-SUBRC = 0. "海关缴款书跳过检查
    RETURN.
  ENDIF.
  " End add

  CALL FUNCTION '/OPT/VIM_PASS_VALUES_TO_GST'
    EXPORTING
      I_1HEAD    = INDEX_DATA
      I_NO_PASS  = 'X'
    IMPORTING
      E_GST_RELE = LV_GSTFLG.
* End of INS VIMI-19726
  CLEAR LS_SW_VERS.
  SELECT SINGLE * FROM CVERS INTO LS_SW_VERS WHERE COMPONENT = 'S4CORE'.
*End S4Hana Change - VIMI-16564
*-----------------------------------------------------------------------
* Check if the Process type is applicable.
*-----------------------------------------------------------------------
***  DATA: ls_invtype TYPE /opt/vim_t100-inv_type,
***        ls_proc TYPE /opt/process_type.
**** Get the country and Invoice Type from the Doc Type
***  SELECT SINGLE inv_type
***  FROM   /opt/vim_t100
***  INTO   ls_invtype
***  WHERE  doctype EQ index_data-doctype.
***  IF sy-subrc EQ 0.
***    CASE ls_invtype.
***      WHEN 'NPO'.
***        ls_proc = ''.
***      WHEN 'LIV'.
***        ls_proc = '155'.
***    ENDCASE.
***
***  ELSE.
***    EXIT.
***  ENDIF.
***
***  CALL FUNCTION '/OPT/VIM_CHECK_PROCESS_TYPE'
***       EXPORTING
***            process_type   = ls_proc
***            index_data     = index_data
***       EXCEPTIONS
***            not_applicable = 1
***            OTHERS         = 2.
***  IF sy-subrc <> 0.
***    EXIT.
***  ENDIF.
*-----------------------------------------------------------------------
*-----------------------------------------------------------------------
*Begin of VIMI-8960 - cnicklas
  CALL FUNCTION '/OPT/VIM_READ_CONSTANT'
    EXPORTING
      PROD_CODE   = '005'
      PARAM_NAME  = 'FAIL_PO_HEADER_SET'
    IMPORTING
      PARAM_VALUE = LV_VALUE
    EXCEPTIONS
      NOT_FOUND   = 1
      OTHERS      = 2.
*End of VIMI-8960 - cnicklas

  SELECT  DOCID
          ITEMID
*          ebeln FROM  /opt/vim_1item                       "VIMI-19726
          EBELN                                             "VIMI-19726
          EBELP                                             "VIMI-19726
          COND_TYPE                                         "VIMI-19726
                FROM /OPT/VIM_1ITEM                         "VIMI-19726
                INTO  TABLE LT_ITEM
                WHERE DOCID = INDEX_DATA-DOCID.
  IF NOT  LT_ITEM[]  IS INITIAL.
    SELECT EBELN
           WAERS
           INTO TABLE LT_EKKO
                  FROM EKKO
                  FOR ALL ENTRIES IN LT_ITEM
                  WHERE EBELN = LT_ITEM-EBELN.

  ENDIF.
***>>>End of Changes 560/2
* Start of INS VIMI-19726
  IF LV_GSTFLG = 'X'.
    LV_SUBRC = 8.
*    LOOP AT lt_item INTO wa_item WHERE cond_type NE space."Commented VIMI-25463
    LOOP AT LT_ITEM INTO WA_ITEM WHERE COND_TYPE NE SPACE AND EBELN IS NOT INITIAL."VIMI-25463
      CLEAR: LT_EK08BN,
             LV_SUBRC.
      CALL FUNCTION 'ME_READ_COND_INVOICE'
        EXPORTING
          I_EBELN = WA_ITEM-EBELN
          I_EBELP = WA_ITEM-EBELP
          I_LIFNR = INDEX_DATA-LIFNR
        TABLES
          XEK08BN = LT_EK08BN.
      IF NOT LT_EK08BN IS INITIAL.
        READ TABLE LT_EK08BN INTO WA_EK08BN WITH KEY KSCHL = WA_ITEM-COND_TYPE.
        IF SY-SUBRC = 0.
          IF INDEX_DATA-WAERS = WA_EK08BN-HWAER OR
             INDEX_DATA-WAERS = WA_EK08BN-WAERS.
          ELSE.
            LV_RAISEFLAG = 'X'.
          ENDIF.
        ENDIF.
      ELSE.
        LV_SUBRC = 4.
      ENDIF.
    ENDLOOP.
    IF LV_RAISEFLAG = 'X'.
      RAISE CHECK_FAILED.
    ELSEIF LV_SUBRC = 0.
      EXIT.
    ENDIF.
  ENDIF.
* End of INS VIMI-19726
*Get the currency

  IF NOT INDEX_DATA-EBELN IS INITIAL.
    SELECT SINGLE WAERS FROM EKKO INTO  L_WAERS
    WHERE EBELN = INDEX_DATA-EBELN .
    IF L_WAERS NE INDEX_DATA-WAERS.
* Start Insert VIMI-13235 AKOESTLI freight condition in item?
      SELECT SINGLE KNUMV FROM EKKO INTO LV_KNUMV
        WHERE EBELN = INDEX_DATA-EBELN
        AND   LOEKZ = ' '.

      IF NOT LV_KNUMV = ' '.
        IF LS_SW_VERS IS INITIAL. " VIMI-16564 not S4Hana system
          SELECT SINGLE KSCHL FROM KONV INTO LV_CNDTYPE
            WHERE KNUMV = LV_KNUMV
            AND   KVSL1 = 'FRE'
            AND   WAERS = INDEX_DATA-WAERS.
*Begin S4Hana Change - VIMI-16564
        ELSE.
          CLEAR LV_CNDTYPE.
          REFRESH LT_KONV.
          LS_FM_NAME = '/OPT/VIM_S4_READ_KONV'.
          SELECT SINGLE FUNCNAME FROM   TFDIR
            INTO   LS_FM_NAME
            WHERE  FUNCNAME EQ LS_FM_NAME.
          IF SY-SUBRC = 0.
            CALL FUNCTION LS_FM_NAME
              EXPORTING
                IV_KNUMV                   = LV_KNUMV
              CHANGING
*               ET_PRC_ELEM                =
                ET_PRC_ELEM_CLASSIC_FORMAT = LT_KONV.
          ENDIF.
          IF NOT LT_KONV[] IS INITIAL.
            READ TABLE LT_KONV INTO LS_KONV WITH KEY KNUMV = LV_KNUMV
                    KVSL1 = 'FRE'
                    WAERS = INDEX_DATA-WAERS.
            IF NOT LS_KONV IS INITIAL.
              LV_CNDTYPE = LS_KONV-KSCHL.
            ENDIF.
          ENDIF.
        ENDIF.
*End S4Hana Change - VIMI-16564
        IF NOT LV_CNDTYPE = ' '.
          LV_RAISE = ' '.
* Start of DEL VIMI-31377
*** Begin of Insertion: VIMI-28119
*          CLEAR lv_subrc.
*          PERFORM cond_curr_check USING index_data-ebeln
*                                        lv_ebelp
*                                        index_data-waers
*                                  CHANGING lv_fr_raise
*                                           lv_subrc.
*          IF lv_subrc NE 0.
*** No conditions fetched,but as DP currency not matches PO currency
*             lv_fr_raise = 'X'.
*          ENDIF.
*** End   of Insertion: VIMI-28119
* End of DEL VIMI-31377
        ELSE.
          LV_RAISE = 'X'.
        ENDIF.
      ENDIF.
********************VIMI_22103 By Chaitanya ***********
*      IF lv_raise = 'X'.               "VIMI_22103
** End Insert VIMI-13235
*        RAISE check_failed.
** Start Insert VIMI-13235
*      ENDIF.
********************VIMI_22103 By Chaitanya ***********
* End Insert VIMI-13235
    ENDIF.
  ENDIF.
  CLEAR: LV_CNDTYPE.                                  "VIMI-28119
*Begin of VIMI-8960 - cnicklas
  SELECT * INTO TABLE LT_LIST FROM /OPT/VIM_1PO_DN
                              WHERE DOCID = INDEX_DATA-DOCID
                              AND   EBELN NE ''.
  IF NOT LT_LIST[] IS INITIAL.
    SELECT EBELN
           WAERS
           KNUMV                                            "VIMI-13235
           INTO TABLE LT_EKKO
               FROM EKKO
               FOR ALL ENTRIES IN LT_LIST
               WHERE EBELN = LT_LIST-EBELN.
    LOOP AT LT_LIST INTO LS_LIST.
      READ TABLE LT_EKKO INTO WA_EKKO WITH KEY EBELN = LS_LIST-EBELN.
      IF WA_EKKO-WAERS NE INDEX_DATA-WAERS.
* Start Insert VIMI-13235 AKOESTLI freight condition in item?
        IF NOT WA_EKKO-KNUMV = ' '.
          IF LS_SW_VERS IS INITIAL. " VIMI-16564 not S4Hana system
            SELECT SINGLE KSCHL FROM KONV INTO LV_CNDTYPE
*              WHERE knumv = lv_knumv                                                   "VIMI_22103
              WHERE KNUMV = WA_EKKO-KNUMV                                               "VIMI_22103
              AND   KVSL1 = 'FRE'
              AND   WAERS = INDEX_DATA-WAERS.
*Begin S4Hana Change - VIMI-16564
          ELSE.
            CLEAR LV_CNDTYPE.
            REFRESH LT_KONV.
            LS_FM_NAME = '/OPT/VIM_S4_READ_KONV'.
            SELECT SINGLE FUNCNAME FROM   TFDIR
              INTO   LS_FM_NAME
              WHERE  FUNCNAME EQ LS_FM_NAME.
            IF SY-SUBRC = 0.
              CALL FUNCTION LS_FM_NAME
                EXPORTING
                  IV_KNUMV                   = LV_KNUMV
                CHANGING
*                 ET_PRC_ELEM                =
                  ET_PRC_ELEM_CLASSIC_FORMAT = LT_KONV.
            ENDIF.
            IF NOT LT_KONV[] IS INITIAL.
              READ TABLE LT_KONV INTO LS_KONV WITH KEY KNUMV = LV_KNUMV
                      KVSL1 = 'FRE'
                      WAERS = INDEX_DATA-WAERS.
              IF NOT LS_KONV IS INITIAL.
                LV_CNDTYPE = LS_KONV-KSCHL.
              ENDIF.
            ENDIF.
          ENDIF.
*End S4Hana Change - VIMI-16564
          IF NOT LV_CNDTYPE = ' '.
            CLEAR LV_RAISE.                                                       "VIMI_22103
* Start of DEL VIMI-31377
*** Begin of Insertion: VIMI-28119
*          CLEAR lv_subrc.
*          PERFORM cond_curr_check USING ls_list-ebeln
*                                        lv_ebelp
*                                        index_data-waers
*                                  CHANGING lv_fr_raise
*                                           lv_subrc.
*          IF lv_subrc NE 0.
*** No conditions fetched,but as DP currency not matches PO currency
*             lv_fr_raise = 'X'.
*          ENDIF.
*** End   of Insertion: VIMI-28119
* End of DEL VIMI-31377
            CONTINUE.
          ENDIF.
        ENDIF.
* End Insert VIMI-13235
        IF NOT LV_VALUE IS INITIAL.
          INDEX_DATA-EBELN = LS_LIST-EBELN.
        ENDIF.
*        RAISE check_failed.                                                      "VIMI_22103
      ENDIF.
    ENDLOOP.
*    ***********VIMI_22103 By Cgogula**************
    IF NOT LV_RAISE IS INITIAL.
      RAISE CHECK_FAILED.
    ENDIF.
*    ***********VIMI_22103 By Cgogula**************
  ENDIF.
*End of VIMI-8960 - cnicklas
  CLEAR: LV_CNDTYPE.                                  "VIMI-28119
* Start of INS VIMI-31377
  LOOP AT LT_ITEM INTO WA_ITEM WHERE COND_TYPE IS NOT INITIAL.
    LV_COND_FLG = 'X'.
    EXIT.
  ENDLOOP.
* End of INS VIMI-31377
***>>>Begin of Changes 560/2
  CLEAR : WA_ITEM.
  LOOP AT LT_ITEM INTO WA_ITEM.
    CLEAR WA_EKKO.
    READ TABLE LT_EKKO INTO WA_EKKO WITH KEY EBELN = WA_ITEM-EBELN.
    IF SY-SUBRC EQ 0.
      IF  WA_EKKO-WAERS NE INDEX_DATA-WAERS.
* Start of INS VIMI-31377
        IF LV_COND_FLG IS INITIAL.
          RAISE CHECK_FAILED.
        ELSE.
* End of INS VIMI-31377
* Start Insert VIMI-13235 AKOESTLI freight condition in item?
          SELECT SINGLE KNUMV FROM EKKO INTO LV_KNUMV
*            WHERE ebeln = index_data-ebeln                 "VIMI-31377
            WHERE EBELN = WA_EKKO-EBELN                     "VIMI-31377
            AND   LOEKZ = ' '.
          IF NOT LV_KNUMV = ' '.
            IF LS_SW_VERS IS INITIAL. " VIMI-16564 not S4Hana system
              SELECT SINGLE KSCHL FROM KONV INTO LV_CNDTYPE
                WHERE KNUMV = LV_KNUMV
                AND   KVSL1 = 'FRE'
                AND   WAERS = INDEX_DATA-WAERS.
*Begin S4Hana Change - VIMI-16564
            ELSE.
              CLEAR LV_CNDTYPE.
              REFRESH LT_KONV.
              LS_FM_NAME = '/OPT/VIM_S4_READ_KONV'.
              SELECT SINGLE FUNCNAME FROM   TFDIR
                INTO   LS_FM_NAME
                WHERE  FUNCNAME EQ LS_FM_NAME.
              IF SY-SUBRC = 0.
                CALL FUNCTION LS_FM_NAME
                  EXPORTING
                    IV_KNUMV                   = LV_KNUMV
                  CHANGING
*                   ET_PRC_ELEM                =
                    ET_PRC_ELEM_CLASSIC_FORMAT = LT_KONV.
              ENDIF.
              CLEAR LS_KONV.                                "VIMI-31377
              IF NOT LT_KONV[] IS INITIAL.
                READ TABLE LT_KONV INTO LS_KONV WITH KEY KNUMV = LV_KNUMV
                        KVSL1 = 'FRE'
                        KSCHL = WA_ITEM-COND_TYPE           "VIMI-31377
                        WAERS = INDEX_DATA-WAERS.
                IF NOT LS_KONV IS INITIAL.
                  LV_CNDTYPE = LS_KONV-KSCHL.
* Start of INS VIMI-31377
                  CLEAR LV_FR_RAISE.
                  EXIT.
                ELSE.
                  LV_FR_RAISE = 'X'.
                  CONTINUE.
* End of INS VIMI-31377
                ENDIF.
              ENDIF.
            ENDIF.
*End S4Hana Change - VIMI-16564
            IF NOT LV_CNDTYPE = ' '.
* Start of DEL VIMI-31377
*** Begin of Insertion: VIMI-28119
*          CLEAR lv_subrc.
*          PERFORM cond_curr_check USING wa_item-ebeln
*                                        wa_item-ebelp
*                                        index_data-waers
*                                  CHANGING lv_fr_raise
*                                           lv_subrc.
*          IF lv_subrc NE 0.
*** No conditions fetched,but as DP currency not matches PO currency
*             lv_fr_raise = 'X'.
*          ENDIF.
*** End   of Insertion: VIMI-28119
* End of DEL VIMI-31377
              CONTINUE.
            ENDIF.
          ENDIF.
* End Insert VIMI-13235
*Begin of VIMI-8960 - cnicklas
          IF NOT LV_VALUE IS INITIAL.
            INDEX_DATA-EBELN = WA_ITEM-EBELN.
          ENDIF.
*End of VIMI-8960 - cnicklas
          RAISE CHECK_FAILED.
        ENDIF.                                              "VIMI-31377
      ENDIF.
    ENDIF.
  ENDLOOP.
***>>>End of Changes 560/2
** Begin of Insertion: VIMI-28119
  IF LV_RAISE IS NOT INITIAL OR
     LV_FR_RAISE IS NOT INITIAL.
    RAISE CHECK_FAILED.
  ENDIF.
** End   of Insertion: VIMI-28119
ENDFUNCTION.
** Begin of Insertion: VIMI-28119
*&---------------------------------------------------------------------*
*&      Form  COND_CURR_CHECK
*----------------------------------------------------------------------*
*      -->PV_EBELN    PO Number
*      -->PV_EBELP    PO item
*      -->PV_WAERS    DP currency
*      <--PV_RAISEFLAG Raise flag
*      <--PV_SUBRC    Return code
*----------------------------------------------------------------------*
FORM COND_CURR_CHECK USING PV_EBELN TYPE EBELN
                           PV_EBELP TYPE EBELP
                           PV_WAERS TYPE WAERS
                     CHANGING PV_RAISEFLAG TYPE FLAG
                              PV_SUBRC     TYPE SY-SUBRC.

  DATA: LT_EK08BN TYPE TABLE OF EK08BN,
        LW_EK08BN TYPE EK08BN,
        LV_LIFNR  TYPE LIFNR.
  CONSTANTS: LC_FREIGHT TYPE KVSL1 VALUE 'FRE',
             LC_TRUE    TYPE CHAR1 VALUE 'X'.

  CALL FUNCTION 'ME_READ_COND_INVOICE'
    EXPORTING
      I_EBELN = PV_EBELN
      I_EBELP = PV_EBELP
      I_LIFNR = LV_LIFNR
    TABLES
      XEK08BN = LT_EK08BN.
  IF NOT LT_EK08BN IS INITIAL.
    LOOP AT LT_EK08BN INTO LW_EK08BN WHERE KVSL1 = LC_FREIGHT.
      IF PV_WAERS = LW_EK08BN-HWAER OR
         PV_WAERS = LW_EK08BN-WAERS.
      ELSE.
        PV_RAISEFLAG = LC_TRUE.
        EXIT.
      ENDIF.
    ENDLOOP.
  ELSE.
** No Delivery cost conditions
    PV_SUBRC = 4.
  ENDIF.
ENDFORM.
** End   of Insertion: VIMI-28119
